package com.css.fxfzfxqh.modules.modelManagement.service.impl;

import cn.hutool.core.util.StrUtil;
import com.css.fxfzfxqh.modules.modelManagement.entity.CasualtyCalculationEntity;
import com.css.fxfzfxqh.modules.modelManagement.param.AddPersonnelCasualtiesParam;
import com.css.fxfzfxqh.modules.modelManagement.param.PagePersonnelCasualtiesParam;
import com.css.fxfzfxqh.modules.modelManagement.repository.PersonnelCasualtiesRepository;
import com.css.fxfzfxqh.modules.modelManagement.service.PersonnelCasualtiesService;
import com.css.fxfzfxqh.modules.modelManagement.vo.PagePersonnelCasualtiesVo;
import com.css.fxfzfxqh.util.CreateGson;
import com.css.fxfzfxqh.util.PlatformDateUtils;
import com.css.fxfzfxqh.util.UUIDGenerator;
import com.css.fxfzfxqh.web.PlatformSessionContext;
import com.css.fxfzfxqh.zcpt.sys.entity.SUser;
import com.css.fxfzfxqh.zcpt.sys.service.SUserService;
import com.css.fxfzfxqh.zcpt.sys.service.SysAreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class PersonnelCasualtiesServiceImpl implements PersonnelCasualtiesService {

    @Autowired
    private PersonnelCasualtiesRepository personnelCasualtiesRepository;

    @Autowired
    private SUserService userService;

    @Autowired
    private SysAreaService areaService;

    @Override
    public int getSizeByName(String modelName) {
        return personnelCasualtiesRepository.getSizeByName(modelName);
    }

    @Override
    public void save(AddPersonnelCasualtiesParam param) {
        CasualtyCalculationEntity entity = paramToEntity(param);
        entity.setId(UUIDGenerator.getUUID());
        String userId = PlatformSessionContext.getUserID();
        entity.setCreateUserId(userId);
        String user = userService.getSUser(userId);
        SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);
        entity.setCreateUserName(sUser.getUserName());
        entity.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        entity.setOrgId(sUser.getOrgId());
        entity.setOrgName(sUser.getOrgName());
        entity.setDelFlag("0");
        personnelCasualtiesRepository.save(entity);
    }

    private CasualtyCalculationEntity paramToEntity(AddPersonnelCasualtiesParam param) {
        return CasualtyCalculationEntity.builder()
                .modelName(param.getModelName())
                .nightClass1(param.getNightClass1())
                .nightClass2(param.getNightClass2())
                .nightClass3(param.getNightClass3())
                .dayClass1(param.getDayClass1())
                .dayClass2(param.getDayClass2())
                .dayClass3(param.getDayClass3())
                .intactInjury(param.getIntactInjury())
                .intactDeath(param.getIntactDeath())
                .slightInjury(param.getSlightInjury())
                .slightDeath(param.getSlightDeath())
                .mediumInjury(param.getMediumInjury())
                .mediumDeath(param.getMediumDeath())
                .seriousInjury(param.getSeriousInjury())
                .seriousDeath(param.getSeriousDeath())
                .destroyInjury(param.getDestroyInjury())
                .destroyDeath(param.getDestroyDeath())
                .isDefault(param.getIsDefault())
                .province(param.getProvince())
                .city(param.getCity())
                .build();
    }

    @Override
    public Map<String, Object> page(PagePersonnelCasualtiesParam param, int curPage, int pageSize) {
        Map<String, Object> page = personnelCasualtiesRepository.page(param, curPage, pageSize);
        List<PagePersonnelCasualtiesVo> list = (List<PagePersonnelCasualtiesVo>) page.get("rows");
        list.forEach(it->{
            it.setProvince(areaService.getAreaNameByAreaId(it.getProvince()));
            if(StrUtil.isNotBlank(it.getCity()) && !"全部".equals(it.getCity()) ){
                it.setCity(areaService.getAreaNameByAreaId(it.getCity()));
            }
        });
        return page;
    }

    @Override
    public void deleteById(String id) {
        personnelCasualtiesRepository.deleteById(id);
    }

    @Override
    public void updateById(AddPersonnelCasualtiesParam param) {
        CasualtyCalculationEntity entity = paramToEntity(param);
        entity.setId(param.getId());
        entity.setUpdateUserId(PlatformSessionContext.getUserID());
        entity.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        personnelCasualtiesRepository.updateById(entity);
    }

    @Override
    public CasualtyCalculationEntity getById(String id) {
        CasualtyCalculationEntity entity = personnelCasualtiesRepository.getById(id);
        if(entity!=null){
            entity.setProvinceName(areaService.getAreaNameByAreaId(entity.getProvince()));
            if(!entity.getCity().equals("全部")){
                entity.setCityName(areaService.getAreaNameByAreaId(entity.getCity()));
            }
            return entity;
        }else {
            return CasualtyCalculationEntity.builder().build();
        }
    }
}
